#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright (c) 2025 粤港澳大湾区（广东）国创中心

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

File: image.py
Desc: Image utility functions for icon creation and SVG conversion.
Author: xumanjia
Date: 2025-08-01
"""
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QPixmap, QPainter,QIcon, QFont
from PyQt5.QtSvg import QSvgRenderer

def create_unicode_icon(symbol, color=Qt.blue, size=64):
    pixmap = QPixmap(size, size)
    pixmap.fill(Qt.transparent)
    painter = QPainter(pixmap)
    painter.setFont(QFont("Arial", int(size*0.8)))
    painter.setPen(color)
    painter.drawText(pixmap.rect(), Qt.AlignCenter, symbol)
    painter.end()
    return QIcon(pixmap)

# def svg_to_ico(svg_path, ico_path, sizes=[16, 32, 48, 64, 128, 256]):
def svg_to_ico(svg_path, sizes=[128]):
    """将SVG转换为多尺寸ICO文件"""
    pixmaps = []
    for size in sizes:
        renderer = QSvgRenderer(svg_path)
        pixmap = QPixmap(QSize(size, size))
        pixmap.fill(Qt.transparent)
        painter = QPainter(pixmap)
        renderer.render(painter)
        painter.end()
        # pixmaps.append(pixmap)
        return QIcon(pixmap)
def svg_to_pix(svg_path, sizes=[32]):
    """将SVG转换为多尺寸pix文件"""
    pixmaps = []
    for size in sizes:
        renderer = QSvgRenderer(svg_path)
        pixmap = QPixmap(QSize(size, size))
        pixmap.fill(Qt.transparent)
        painter = QPainter(pixmap)
        renderer.render(painter)
        painter.end()
        # pixmaps.append(pixmap)
        return pixmap